package com.car.action.admin;

import java.util.List;
import java.util.Random;

import com.car.common.model.Banner;
import com.car.common.model.BlackList;
import com.car.common.model.Card;
import com.car.common.model.CardDiscuss;
import com.car.common.model.CardHot;
import com.car.common.model.Exchange;
import com.car.common.model.PrizeRecord;
import com.car.common.model.Users;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.utils.MD5Util;
import com.utils.StringUtil;
import com.utils.SystemClock;
import com.utils.UuidUtil;

public class BlackListController extends Controller{

	public void index() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
		String sql = "select * ";
		String title = "";
		if(!StringUtil.empty(getPara("title"))) {
			title = " where title like '%" + getPara("title") +"%'";
		}
		String andSql = " from black_list " + title + " order by id desc ";
		Page<BlackList> pageData = BlackList.dao.paginate(page, 15, sql,andSql);
		setAttr("page", pageData);
		setAttr("title", getPara("title"));
		render("index.html");
	}
	
	public void banner_list() {
		setAttr("list", Banner.dao.getAll2());
		render("banner_list.html");
	}
	
	public void banner_online() {
		Db.update("update banner set status = 1 where id = " + getPara("id"));
		renderText("操作成功!");
	}
	
	public void banner_offline() {
		Db.update("update banner set status = 2 where id = " + getPara("id"));
		renderText("操作成功!");
	}
	
	public void del() {
		Db.delete("delete from black_list where id = " + getPara("id"));
		renderText("操作成功!");
	}
	
	public void del_select() {
		String[] ids = getPara("ids").split(",");
 		for(String str :ids) {
 			Db.delete("delete from black_list where id = " + str);
 		}
 	    renderText("操作成功!");
	}
	
	public void save() {
		BlackList one = BlackList.dao.findById(getPara("id"));
		boolean updateFlg = false;
		if (one == null) {
			 one = new BlackList();
			 one.setCreateTime(SystemClock.now()/1000L);
			 updateFlg = true;
		}
		one.setTitle(getPara("title"));
		if (updateFlg) {
			one.save();
		} else {
			one.update();
		}
		renderText("操作成功!");
	}
	
	
	public void zan_discuss_list() {
		String sql = "select id,content,type,case type when '3' then '打招呼' when '4' then '赞美' when '5' then '可爱' when '6' then '嫌弃' when '7' then '求交往'  else '其他' end as type_name  from card_discuss where card_id = 0 and user_id = 0 ";
		if(!StringUtil.empty(getPara("type"))) {
			sql += " and type = " + getPara("type");
		}
		sql +=  " order by id desc ";
		List<CardDiscuss> discuss_list = CardDiscuss.dao.find(sql);
		setAttr("list", discuss_list);
		setAttr("type", getPara("type"));
		
		render("zan_discuss_list.html");
	}
	
	public void del_zan_discuss() {
		Db.delete("delete from card_discuss where id = " + getPara("id"));
 	    renderText("操作成功!");
	}
	
	public void zan_discuss() {
		CardDiscuss one = new CardDiscuss();
		one.setCreateTime(0L);
		one.setCardId(0);
		one.setUserId(0);
		one.setType(getParaToInt("type"));
		one.setContent(getPara("content"));
		one.save();
		renderText("操作成功!");
	}
	
	public void exchange_list() {
		String sql = "select * from exchange where status = 0 and user_id = 0 ";
		sql +=  " order by id desc ";
		List<Exchange> list = Exchange.dao.find(sql);
		setAttr("list", list);
		render("exchange_list.html");
	}
	
	public void del_exchange() {
		Db.delete("delete from exchange where id = " + getPara("id"));
 	    renderText("操作成功!");
	}
	
	public void exchange() {
		Exchange one = new Exchange();
		one.setCode(UuidUtil.get32Id().substring(0, 12));
		one.setUserId(0);
		one.setStatus(0);
		one.setTitle(getPara("title"));
		one.setDays(getParaToInt("days"));
		one.setHot(getParaToInt("hot"));
		one.setTimes(getParaToInt("times"));
		one.setExpiretime(0L);
		one.setCreateTime(SystemClock.now()/1000L);
		one.setUpdateTime(SystemClock.now()/1000L);
		one.save();
		renderText("操作成功!");
	}
	
	
	public void prize_list() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        String sql = "select u.nick_name, FROM_UNIXTIME(p.create_time,'%Y-%m-%d %H:%i') as create_time_string,p.title,p.user_id ";
        
        String andSql = " from prize_record as p LEFT JOIN users as u on p.user_id = u.id ";
        
        if(!StringUtil.empty(getPara("user_id"))) {
        	andSql += " and p.user_id = " + getPara("user_id");
        }
        andSql += " order by p.id desc ";
        setAttr("page", PrizeRecord.dao.paginate(page, 30, sql, andSql));
        setAttr("user_id", getPara("user_id"));
		render("prize_list.html");
	}
}

